//定义js命名空间
Ext.namespace('Infrastructure.InfraIdcard');

//InfraIdcard FieldArray
Infrastructure.InfraIdcard.BaseFieldsArray = new Array(
	{ name: 'Id', type: 'string' },
	{ name: 'CardUid', type: 'string' },
	{ name: 'CardInfo', type: 'string' },
	{ name: 'Type', type: 'int' },
	{ name: 'ItemId', type: 'string' },
	{ name: 'DeptId', type: 'string' },
	{ name: 'Department.Name', mapping: 'Department.Name' },
	{ name: 'State', type: 'string' },
	{ name: 'Version', type: 'int' },
	{ name: 'IsDelete', type: 'bool' }
);

//InfraIdcard JsonReader
Infrastructure.InfraIdcard.JsonReader = Ext.extend(Infrastructure.BaseJsonReader, {
	constructor: function (cfg) {
		cfg = cfg || {};
		Infrastructure.InfraIdcard.JsonReader.superclass.constructor.call(this, Ext.apply({
			fields: Infrastructure.InfraIdcard.BaseFieldsArray
		}, cfg));
	}
});

//InfraIdcard JsonStore
Infrastructure.InfraIdcard.JsonStore = Ext.extend(Infrastructure.BaseJsonStore, {
	constructor: function (cfg) {
		cfg = cfg || {};
		Infrastructure.InfraIdcard.JsonStore.superclass.constructor.call(this, Ext.apply({
			url: '/InfraIdcard/GetPlist',
			params: {
				start: 0,
				limit: ALL_PAGESIZE_SETTING
			},
			remoteSort: true,
			fields : Infrastructure.InfraIdcard.BaseFieldsArray,
			listeners: {
				'remove': function (thiz, record, index) {
					Ext.Ajax.request({
						url: '/InfraIdcard/Delete?Id=' + record.data.Id,
						success: function (response, opts) {
							var result = Ext.util.JSON.decode(response.responseText);
							if (result.success == false) {
								JsHelper.ShowError(result.msg);
								this.insert(0, record);
							}
						}
					});
				}
			}
		}, cfg));
	}
});

//InfraIdcard GridPanel
Infrastructure.InfraIdcard.GridPanel = Ext.extend(Infrastructure.BaseGridPanel, {
	// User property
	//基本处理Url
	baseUrl: '/InfraIdcard/',
	//提交保存Url
	formSubmitUrl: '/InfraIdcard/SaveOrUpdate',
	//提交保存提示信息
	formSubmitMsg: '确定要保存吗?',
	//状态
	itemState: '',

	// Property
	store: new Infrastructure.InfraIdcard.JsonStore(),
	// stateful: true,
	// stateId: 'Infrastructure.InfraIdcard.GridPanel',

	//constructor
	initComponent: function () {
		//prepare
		var itemGrid = this;
		var tbarMask = this.tbarMask;
		var cmdAddEntity = this.onAddEntity;

		//set store params
		this.store.setBaseParam('state', this.itemState);

		// The filter.
		var gridFilter = new Ext.ux.grid.GridFilters({
			encode: true,
			filters: [
				{ dataIndex: 'CardUid', type: 'string' },
				{ dataIndex: 'CardInfo', type: 'string' },
				{ dataIndex: 'Type', type: 'list', options: JsHelper.GetFilterListData(Infrastructure.Params.IDCardTypeStore.data.items, 'Code', 'Name') },
				{ dataIndex: 'ItemId', type: 'string' },
				{ dataIndex: 'Department.Name', type: 'list', options: JsHelper.GetFilterListData(Infrastructure.Params.DeptStore.data.items, 'Name', 'Name') },
				{ dataIndex: 'State', type: 'list', options: JsHelper.GetFilterListData(Infrastructure.Params.IDCardStateStore.data.items, 'Code', 'Name') },
				{ dataIndex: 'Createtime', type: 'date' }
			]
		});

		// selModel
		var selModel = new Ext.grid.CheckboxSelectionModel({
			//header: '',
			//singleSelect: true,
			listeners: {
				'selectionchange': function (sm) {
					// 控制工具栏按钮是否可用
					itemGrid.delBtn.setDisabled(sm.getCount() < 1);
				}
			}
		});

		// columnModel 对于不常需要显示的 hidden: true 
		var gridColumn = new Ext.grid.ColumnModel({
			columns: [
				new Ext.grid.RowNumberer(),
				selModel,
				{ header: "IC卡编号", dataIndex: 'CardUid' },
				{ header: "IC卡类型", dataIndex: 'Type',
					renderer: function (v) {
						return Infrastructure.Params.IDCardTypeStore.getById(v).get('Name');
					}
				},
				{ header: "所属机构", dataIndex: 'Department.Name' },
				{ header: "ID卡状态", dataIndex: 'State', 
					renderer: function (v) {
						return v == "" ? "" : Infrastructure.Params.IDCardStateStore.getById(v).get('Name');
					}
				}
			]
		});

		// PageBar
		var pagebBar = new Infrastructure.BasePagingToolbar({
			store: this.store,
			plugins: this.plugins
		});

		Ext.apply(this, {
			plugins: [gridFilter],
			sm: selModel,
			cm: gridColumn,
			tbar: [{
				xtype: 'tbaraddbtn',
				handler: cmdAddEntity,
				scope: itemGrid
			}, '-', {
				xtype: 'tbardelbtn',
				handler: function(){
				   itemGrid.delEntity();
				}
			}, '-', {
				xtype: 'textfield',
				width: 150,
				id: 'q_query'
			}, {
				xtype: 'tbarqrybtn',
				handler: function(e){
					itemGrid.qryList(itemGrid.q_query.getValue());
				},
				scope: itemGrid
			}, '-'],
			bbar: pagebBar,
			listeners: {
				'rowdblclick': function (grid, rowIndex, e) {
					this.onLoadEntity(grid.store.getAt(rowIndex), false);
				}
			}
		});

		Infrastructure.InfraIdcard.GridPanel.superclass.initComponent.call(this);
	},

	/****************** 
	* user function 
	******************/

	// 添加
	onAddEntity: function () {
		var itemGrid = this;
		var itemEditForm = new Infrastructure.InfraIdcard.EditForm({
			isEdit : true,
			listeners: {
				'afterSave': function (ele, rt) {
					if (rt) {
						var itemGridStore = itemGrid.store;
						itemGridStore.reload();
						JsHelper.ShowInfo('添加信息成功!');
					}
				}
			}
		});
		var itemEditWin = new Infrastructure.BaseWindow({
			title: 'IC卡信息',
			iconCls: 'id_card',
			width: 300,
			items: [itemEditForm]
		});
		itemEditWin.show();
	},

	// 读取信息显示
	onLoadEntity: function (selRecord, isEdit) {
		var itemGrid = this;
		var selId = selRecord.data.Id;
		var itemEditForm = new Infrastructure.InfraIdcard.EditForm({
			isEdit: isEdit,
			itemId: selId,
			dataRecord: selRecord,
			submitUrl: this.formSubmitUrl,
			submitMsg: this.formSubmitMsg,
			waitMsg: '数据加载中...',
			listeners: {
				'afterSave': function (ele, rt) {
					if (rt) {
						itemGrid.store.reload();
						JsHelper.ShowInfo('信息更新成功!');
					}
				}
			}
		});
		var itemEditWin = new Infrastructure.BaseWindow({
			title: 'IC卡信息',
			iconCls: 'id_card',
			width: 300,
			items: [itemEditForm]
		});
		itemEditWin.show();
		itemEditForm.form.loadRecord(selRecord);
	}
});

//InfraIdcard EditForm
Infrastructure.InfraIdcard.EditForm = Ext.extend(Infrastructure.BaseForm, {
	isEdit: false, //是否允许编辑
	submitUrl: '/InfraIdcard/SaveOrUpdate',
	submitMsg: '确定要保存吗?',

	initComponent: function () {
		if (this.dataRecord != null)
			this.dataId = this.dataRecord.data.Id;

		var itemEditForm = this;

		//Form第一列
		// 定义表单列
		var firstColumn = [{
			fieldLabel: "所属机构",
			xtype: 'deptcombo',
			hiddenName: 'DeptId'
		}, {
			fieldLabel: "IC卡类型",
			xtype: 'infra_localcombo',
			store: Infrastructure.Params.IDCardTypeStore,
			hiddenName: 'Type'
		}, {
			fieldLabel: "IC卡编号",
			xtype: 'infra_idcard_textfield',
			name: 'CardUid',
			plugins: [Ext.ux.plugins.RemoteValidator],
			rvOptions: {
				url: '/InfraIdcard/IsFieldExist',
				params: {
					'FieldName': 'CardUid',
					'Id': this.dataId == null ? '-1' : this.dataId
				}
			}
		}];

		Ext.apply(this, {
			defaults: {
				width: 150,
				allowBlank: false
			},
			items: firstColumn,
			buttons: [{
				xtype: 'btnsubmit',
				handler: function(){
					itemEditForm.saveItem(itemEditForm.submitUrl)
				},
				hidden: this.submitUrl == '',
				scope: itemEditForm
			}, {
				xtype: 'btncancle',
				handler: function () {
					itemEditForm.ownerCt.destroy();
				}
			}],
			reader: new Infrastructure.InfraIdcard.JsonReader()
		});

		Infrastructure.InfraIdcard.EditForm.superclass.initComponent.call(this);

		//定义保存后执行的事件
		this.addEvents('afterSave');
	}
});

//InfraIdcard DetailWin
Infrastructure.InfraIdcard.EditWin = Ext.extend(Infrastructure.BaseWindow, {
	title: 'IC卡信息',
	iconCls: 'id_card',
	width: 300
});
